{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Installing hub:\u001b[35m/\u001b[0m\u001b[35m/guardrails/\u001b[0m\u001b[95mtwo_words...\u001b[0m\n",
      "✅Successfully installed guardrails/two_words!\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "!guardrails hub install hub://guardrails/two_words --quiet"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Input Validation\n",
    "\n",
    "Guardrails supports validating inputs (prompts, instructions, msg_history) with string validators."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In XML, specify the validators on the `prompt` or `instructions` tag, as such:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "is_executing": true
   },
   "outputs": [],
   "source": [
    "from guardrails import Guard\n",
    "\n",
    "rail_spec = \"\"\"\n",
    "<rail version=\"0.1\">\n",
    "<prompt\n",
    "    validators=\"hub://guardrails/two_words\"\n",
    "    on-fail-two-words=\"exception\"\n",
    ">\n",
    "This is not two words\n",
    "</prompt>\n",
    "<output type=\"string\">\n",
    "</output>\n",
    "</rail>\n",
    "\"\"\"\n",
    "\n",
    "guard = Guard.from_rail_string(rail_spec)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When `fix` is specified as the on-fail handler, the prompt will automatically be amended before calling the LLM.\n",
    "\n",
    "In any other case (for example, `exception`), a `ValidationError` will be returned in the outcome."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "is_executing": true
   },
   "outputs": [],
   "source": [
    "import openai\n",
    "from guardrails.errors import ValidationError\n",
    "\n",
    "try:\n",
    "    guard(\n",
    "        openai.chat.completions.create,\n",
    "    )\n",
    "except ValidationError as e:\n",
    "    print(e)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When using pydantic to initialize a `Guard`, input validators can be specified by composition, as such:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/zaydsimjee/workspace/guardrails/docs/examples/.venv/lib/python3.10/site-packages/IPython/core/interactiveshell.py:3577: FutureWarning: The `with_prompt_validation` method is deprecated,\n",
      "        and will be removed in 0.5.x. Instead, please use\n",
      "        `Guard().use(YourValidator, on='prompt')`.\n",
      "  exec(code_obj, self.user_global_ns, self.user_ns)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Validation failed for field with errors: Value must be exactly two words\n"
     ]
    }
   ],
   "source": [
    "from guardrails.hub import TwoWords\n",
    "from pydantic import BaseModel\n",
    "\n",
    "\n",
    "class Pet(BaseModel):\n",
    "    name: str\n",
    "    age: int\n",
    "\n",
    "\n",
    "guard = Guard.from_pydantic(Pet)\n",
    "guard.with_prompt_validation([TwoWords(on_fail=\"exception\")])\n",
    "\n",
    "try:\n",
    "    guard(\n",
    "        openai.chat.completions.create,\n",
    "        prompt=\"This is not two words\",\n",
    "    )\n",
    "except ValidationError as e:\n",
    "    print(e)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
